<template>
    <div class="page-register">
        <div class="section-login">
            <div class="box-login-wrapper">
            <div class="box-login card">
                <div class="card-body">
                    <!--<form class="form-horizontal form-material" id="loginform" action="index.html">-->
                    <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="0px" class="demo-ruleForm">
                        <h3 class="box-title m-b-20">注册用户</h3>
                        <div class="form-group ">
                            <el-form-item prop="username">
                                <el-input v-model="ruleForm.username" placeholder="用户名"></el-input>
                            </el-form-item>
                        </div>
                        <div class="form-group">
                            <el-form-item prop="email">
                                <el-input type="email" placeholder="邮件" v-model="ruleForm.email" @keyup.enter.native="submitForm('ruleForm')"></el-input>
                            </el-form-item>
                        </div>
                        <div class="form-group">
                            <el-form-item prop="password">
                                <el-input type="password" placeholder="密码" v-model="ruleForm.password" @keyup.enter.native="submitForm('ruleForm')"></el-input>
                            </el-form-item>
                        </div>
                        <div class="form-group">
                            <el-form-item prop="password">
                                <el-input type="password" placeholder="确认密码" v-model="ruleForm.repassword" @keyup.enter.native="submitForm('ruleForm')"></el-input>
                            </el-form-item>
                        </div>
                        <el-row>
                            <el-col :span="24">
                                <el-button type="primary" class="btn btn-info btn-lg btn-block text-uppercase waves-effect waves-light" @click="submitForm('ruleForm')">注 册</el-button>
                            </el-col>
                        </el-row>
                        <div class="form-group m-b-0">
                            <div class="col-sm-12 text-center">
                                <p>已有帐号？<a href="#/login" class="text-info m-l-5">帐号登录</a></p>
                            </div>
                        </div>
                    </el-form>
                </div>
            </div>
            </div>
        </div>
    </div>
</template>
<script>
    import http from "../../lib/http"
//    import ElButton from "../../../node_modules/element-ui/packages/button/src/button.vue";
//    import ElRow from "../../../node_modules/element-ui/packages/row/src/row.vue";
    //http()["get"]
    export default {
//        components: {
//            ElRow,
//            ElButton},
        data: function(){
            return {
                loading : false,
                error_msg : "",
                tip:false,

                ruleForm: {
                    username: '',
                    password: '',
                    repassword : '',
                    email : '',
                    rememberPwd : false
                },
                rules: {
                    username: [
                        { required: true, message: '请输入用户名', trigger: 'blur' }
                    ],
                    email: [
                        { required: true, message: '请输入邮箱', trigger: 'blur' }
                    ],
                    password: [
                        { required: true, message: '请输入密码', trigger: 'blur' }
                    ],
                    repassword: [
                        { required: true, message: '请输入确认密码', trigger: 'blur' }
                    ]
                }
            }
        },
        created : function(){
//            http.alive(this,function(){
//                location.href="#/"
//            },false)
        },
        methods: {

            submitForm(formName) {
                this.loading = true
                const self = this;
                self.$refs[formName].validate((valid) => {
                    self.loading = false
                    if (valid) {
                        http.post(self,"register/student",{"username":self.ruleForm.username,"password":self.ruleForm.password,"repassword":self.ruleForm.repassword,"email":self.ruleForm.email}).then(function(res){
                            console.log(res.data)
                            if(res.data.code == "fail"){
                                self.$notify.error({
                                    title: '错误',
                                    message: '请输入正确信息'
                                });
                            }
                            else{
                                self.$notify.success({
                                    title: '成功',
                                    message: '注册成功'
                                });
                                //localStorage.setItem('access_token',res.data.data.access_token);
                                //localStorage.setItem('ms_username',self.ruleForm.username);
                                self.$router.push('/');
                            }
                        })
                        //self.$http.post({""})
                        //localStorage.setItem('ms_username',self.ruleForm.username);
                        //self.$router.push('/readme');
                    } else {
//                        self.$notify.error({
//                            title: '错误',
//                            message: '请输入正确信息'
//                        });
                        //console.log('error submit!!');
                        return false;
                    }
                });
            }
        }
    }
</script>
